from django.db import models
from utils.models import BaseModel
from apps.users.models import User, Address
from apps.products.models import Product


# Create your models here.
# 订单类
class OrderInfo(BaseModel):
    """订单信息"""
    PAY_METHOD_ENUM = {
        "WeChat": 1,
        "Alipay": 2
    }
    PAY_METHOD_CHOICE = (
        (1, "微信"),
        (2, "支付宝")
    )
    STATUS_ENUM = {
        '待配送': 1,
        '配送中': 2,
        '已完成': 3,
        '已取消': 4,
        '退款中': 5,

    }
    STATUS_CHOICE = (
        (1, '待配送'),
        (2, '配送中'),
        (3, '已完成'),
        (4, '已取消'),
        (5, '退款中')
    )
    order_id = models.CharField(max_length=70, primary_key=True)
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    address = models.ForeignKey(Address, on_delete=models.PROTECT)
    total_count = models.IntegerField(default=1)
    total_amount = models.DecimalField(max_digits=5, decimal_places=2)
    pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICE, default=1)
    status = models.SmallIntegerField(choices=STATUS_CHOICE, default=1)
    is_delete = models.BooleanField(default=False)

    class Meta:
        managed = True
        db_table = 'order_info'

    def __str__(self):
        return self.order_id


class OrderGoods(BaseModel):
    """订单商品"""
    order = models.ForeignKey(OrderInfo, related_name='goods', on_delete=models.CASCADE, verbose_name="订单")
    product = models.ForeignKey(Product, on_delete=models.PROTECT)
    count = models.IntegerField(default=1)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    class Meta:
        db_table = "order_goods"

    def __str__(self):
        return self.product.product_name
